/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package cn.edu.thu.log.clean;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import cn.edu.thu.log.read.LogBufferModified;
import cn.edu.thu.log.web.service.WebConfigReadService;
import cn.edu.thu.log.web.service.impl.WebConfigReadServiceImpl;

/**
 * clean the log record
 * 
 * @author wan
 * 
 */
public class LogClean {
	private WebConfigReadService webconfigreadservice;
	private Map<String, String> crule;

	// private int flag=1;//默认符合格式要求
/**
 * 
 */
	public LogClean() {
		webconfigreadservice = WebConfigReadServiceImpl.instance();

		// webconfigreadservice.readWebConfig(PanGuConstants.CLEAN_CONFIGFILE);

		crule = new HashMap<String, String>();
		crule.putAll(webconfigreadservice.getLogCleanList());

	}
/**
 * 
 * @param record parameter
 * @return
 */
	public boolean logClean(LogBufferModified record) {
		// 判断标签和内容的字段数量是否一致

		int flag = 1;
		Iterator<Entry<String, String>> it = crule.entrySet().iterator();
		// System.out.println(it.hasNext());

		while (it.hasNext()) {
			// System.out.println("进入logclean，遍历logtag");
			Entry<String, String> entry = it.next();
			// 获得每一个需要清洗的tag
			String tagname = entry.getKey();
			// 获取对应tag存储在logTagList中的位置，以便在logContent中找到对应的值
			
			String content = record.getContent("tagname");
			
			// int index = record.getLogTagList().indexOf(tagname);
			// String content = record.getLogContent().get(index).toString();
			String tagformat = entry.getValue();

			// 最終將content按照正則表達式tagformat的形式進行匹配,必须全匹配，否则删除
			Pattern pattern = Pattern.compile(tagformat);
			// Pattern pattern =
			// Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
			Matcher matcher = pattern.matcher(content);
			// 如果不匹配，则后面的字段不用检查，直接返回整个记录不符合要求，false
			if (!matcher.matches()) {
				flag = 0;
				// System.out.println("字段" + tagname + "的内容 " + content +
				// "不符合规则"
				// + tagformat);
				break;
			}
			// 如果匹配，则flag保持为1
		}
		return !(flag == 0);
	}

	// 获取缩写
	/**
	 * 
	 */
	public void getAbbreviation() {

	}

}
